import { configureStore } from '@reduxjs/toolkit';
import ballsSlice from './balls/balls.slice';
import  userRegistrationSlice  from './auth/registration.slice';
import  userAuthorizationSlice, { KEY_STORAGE }  from './auth/authorisation.slice';
import { saveStorage } from './storage';
import cartSlice from './cart/cart.slice';
import  userMeSlice  from './user/user.slice';


export const store = configureStore({
	reducer: {
		balls: ballsSlice, 
		registration: userRegistrationSlice,
		authorization: userAuthorizationSlice,
		cart: cartSlice,
		userMe: userMeSlice
	}
});

store.subscribe(() => {
	saveStorage({ jwt: store.getState().authorization.user.token }, KEY_STORAGE);
});


export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
